﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.Linq;
using Biz_Dal.Dal;

namespace Biz_Dal.Biz
{
    public class BizSach
    {
        #region "Khởi tạo"
        public BizSach()
        { 
        
        }

        public BizSach(int _idSach)
        {
            this._idSach = _idSach;
        }

        public BizSach(int _idSach, string _maSach, string _tenSach, string _tacGia,
                Binary _hinhSach, double _giaSach, string _ndSach, int _idTheLoai, int _idNXB)
        {
            this._idSach= _idSach;

            this._maSach = _maSach;

            this._tenSach= _tenSach;

            this._tacGia= _tacGia;

            this._hinhSach= _hinhSach;

            this._giaSach= _giaSach;

            this._ndSach= _ndSach;

            this._idTheLoai= _idTheLoai;

            this._idNXB= _idNXB;
        }

        public BizSach(int _idSach, string _maSach, string _tenSach, string _tacGia,
                Binary _hinhSach, double _giaSach, string _ndSach, int _idTheLoai, int _idNXB, BizNXB _nxb, BizTheLoai _theLoai)
        {
            this._idSach = _idSach;

            this._maSach = _maSach;

            this._tenSach = _tenSach;

            this._tacGia = _tacGia;

            this._hinhSach = _hinhSach;

            this._giaSach = _giaSach;

            this._ndSach = _ndSach;

            this._idTheLoai = _idTheLoai;

            this._idNXB = _idNXB;

            this._nxb = _nxb;

            this._theLoai = _theLoai;
        }

        public BizSach(int _idSach, string _maSach, string _tenSach, string _tacGia,
                System.Data.Linq.Binary _hinhSach, double _giaSach, string _ndSach, BizNXB _nxb, BizTheLoai _theLoai)
        {
            this._idSach = _idSach;

            this._maSach = _maSach;

            this._tenSach = _tenSach;

            this._tacGia = _tacGia;

            this._hinhSach = _hinhSach;

            this._giaSach = _giaSach;

            this._ndSach = _ndSach;

            this._nxb = _nxb;

            this._theLoai = _theLoai;
        }
        #endregion

        #region "Thuộc tính"
        private int _idSach;

        private string _maSach;

        private string _tenSach;

        private string _tacGia;

        private Binary _hinhSach;

        private byte[] _byteHinhSach;

        private double _giaSach;

        private string _ndSach;

        private int _idTheLoai;

        private int _idNXB;

        private BizNXB _nxb;

        private BizTheLoai _theLoai;

        private string tenNXB;

        private string tenTheLoai;
        
        public int idSach
        {
            get
            {
                return this._idSach;
            }
            set
            {
                this._idSach = value;
            }
        }

        public string maSach
        {
            get
            {
                return this._maSach;
            }
            set
            {
                this._maSach = value;
            }
        }

        public string tenSach
        {
            get
            {
                return this._tenSach;
            }
            set
            {
                this._tenSach = value;
            }
        }

        public string tacGia
        {
            get
            {
                return this._tacGia;
            }
            set
            {
                this._tacGia = value;
            }
        }


        public Binary hinhSach
        {
            get
            {
                return this._hinhSach==null?new Binary(byteHinhSach):_hinhSach;
            }
            set
            {
             
                this._hinhSach = value;
            }
        }

        private byte[] byteHinhSach
        {
            get
            {
                return this._byteHinhSach;
            }
            set
            {

                this._byteHinhSach = value;
            }
        }

      
        public double giaSach
        {
            get
            {
                return this._giaSach;
            }
            set
            {
                this._giaSach = value;
            }
        }

        
        public string ndSach
        {
            get
            {
                return this._ndSach;
            }
            set
            {
                this._ndSach = value;
            }
        }

        
        public int idTheLoai
        {
            get
            {
                return this._idTheLoai;
                
            }
            set
            {
                this._idTheLoai = value;
            }
        }

        
        public int idNXB
        {
            get
            {
                return this._idNXB;
            }
            set
            {
                this._idNXB = value;
            }
        }

        public string TenNXB
        {
            get
            {
                return nxb == null ? string.Empty : _nxb.tenNXB;
            }
        }

        public string TenTheLoai
        {
            get
            {
                return theLoai == null ? string.Empty : _theLoai.tenTheLoai;
            }
        }

        public BizNXB nxb
        {
            get
            {
                return this._nxb;
            }
            set
            {
                this._nxb = value;
            }
        }

        public BizTheLoai theLoai
        {
            get
            {
                return this._theLoai;
            }
            set
            {
                this._theLoai = value;
            }
        }

        #endregion

        //DalSach sach = new DalSach();

        public static int CountListSach()
        {
            try
            {
                return DalSach.CountListSach();
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //lấy id mới
        public static int IdMaxSach()
        {
            try
            {
                return DalSach.IdMaxSach();
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //Lấy danh sách tất cả các sách
        public static List<BizSach> getListSachAll()
        {
            try
            {
                return DalSach.getListSachAll();
            }
            catch
            {
                throw new Exception("Error!");
            }
        }


        //Chi tiết sách
        public static BizSach getSachByMaSach(string maSach)
        {
            try
            {
                return DalSach.getSachByMaSach(maSach);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        public static BizSach getSachById(int idSach)
        {
            try
            {
                return DalSach.getSachById(idSach);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        public static List<BizSach> getListSachByidTheLoai(int idTheLoai)
        {
            try
            {
                return DalSach.getListSachByidTheLoai(idTheLoai);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //Lấy danh sách tất cả các sách có tên được nhập vào
        public static List<BizSach> getListSachByName(string name)
        {
            try
            {
                return DalSach.getListSachByName(name);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //Tìm kiếm nâng cao
        public static List<BizSach> getListSachByMulti(string tenSach, int idTheLoai, int idNXB, double price_start, double price_end)
        {
            try
            {
                return DalSach.getListSachByMulti(tenSach, idTheLoai,idNXB, price_start, price_end);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //Danh sách gần đúng các sách có mã sách được nhập vào
        public static List<BizSach> getLikeSachByMa(string maSach)
        {
            try
            {
                return DalSach.getLikeSachByMa(maSach);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //Lấy danh sách gần đúng tất cả các sách có tên được nhập vào
        public static List<BizSach> getListLikeSachByName(string name)
        {
            try
            {
                return DalSach.getListLikeSachByName(name);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //Lấy danh sách gần đúng tất cả các sách có tác giả được nhập vào
        public static List<BizSach> getListLikeSachByTacGia(string tgia)
        {
            try
            {
                return DalSach.getListLikeSachByTacGia(tgia);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //Thêm sách
        public static void insertSach(BizSach vtSach)
        {
            try
            {
                DalSach.insertSach(vtSach);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //Xóa sách
        public static void deleteSach(int idSach)
        {
            try
            {
                DalSach.deleteSach(idSach);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //Sửa sách
        public void updateSach()
        {
            try
            {
                DalSach.updateSach(this);
            }
            catch
            {
                throw new Exception("Error!");
            }
        }

        //Sửa ảnh
        public static void updateImage(int id, byte[] filebyte)
        {
            try
            {
                DalSach.updateImage(id, new System.Data.Linq.Binary(filebyte));
            }
            catch
            {
                throw new Exception("Error!");
            }
        }
    }
}
